import 'package:automated_test/common/constant.dart';
import 'package:automated_test/common/custom_router.dart';
import 'package:automated_test/l10n/DemoLocalizations.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:local_auth/local_auth.dart';

import '../home/home.dart';

///登录页
class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

/// 登录通过客户账号与密码登录
class _LoginPageState extends State<LoginPage> {
  @override
  void initState() {
    super.initState();

    ///设置系统通知栏背景透明
    SystemChrome.setSystemUIOverlayStyle(
        SystemUiOverlayStyle(statusBarColor: Colors.transparent));

    ///设置只显示系统顶部通知栏，隐藏底部导航栏
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: [SystemUiOverlay.top]);
  }

  @override
  Widget build(BuildContext context) {
    ///初始化屏幕适配工具
    ScreenUtil.init(
        BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width,
            maxHeight: MediaQuery.of(context).size.height),
        designSize: Size(384, 832),
        orientation: Orientation.portrait);

    return Scaffold(
      // appBar: AppBar(
      //   title: const Text(
      //     "登录",
      //     style: TextStyle(fontSize: 20),
      //   ),
      //   centerTitle: true,
      // ),
      body: LoginContent(),
    );
  }
}

class LoginContent extends StatefulWidget {
  @override
  _LoginContentState createState() => _LoginContentState();
}

class _LoginContentState extends State<LoginContent> {
  ///账号编辑控制器
  final TextEditingController _accountController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  dispose() {
    ///释放账号控制器
    _accountController.dispose();
    super.dispose();
  }

  ///账号输入框
  Widget _buildAccountTextField() => Container(
        height: Dimension.h50,
        width: Dimension.w300,
        child: TextFormField(
          textAlignVertical: TextAlignVertical.bottom,
          controller: _accountController,
          style: TextStyle(fontSize: Dimension.sp24),
          decoration: InputDecoration(
            prefixIcon: Icon(
              Icons.person,
              color: Colors.black,
              size: Dimension.sp24,
            ),
            prefixIconConstraints: BoxConstraints(
                minWidth: Dimension.sp48, minHeight: Dimension.sp48),
            hintText: DemoLocalizations.of(context).accountHint,
            hintStyle: TextStyle(color: Colors.white, fontSize: Dimension.sp24),
            enabledBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.white, width: Dimension.w1),
              borderRadius: BorderRadius.all(Radius.circular(Dimension.r25)),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.black, width: Dimension.w1),
              borderRadius: BorderRadius.all(Radius.circular(Dimension.r25)),
            ),
          ),
        ),
      );

  ///登录按钮
  Widget _buildSubmitButton() => MaterialButton(
        onPressed: () async {
          var auth = LocalAuthentication();
          // if (await auth.canCheckBiometrics) {
          //   var authenticated = await auth.authenticate(
          //       localizedReason: "指纹验证",
          //       useErrorDialogs: false,
          //       androidAuthStrings: AndroidAuthMessages(cancelButton: "取消"));
          //   if (authenticated) {
          //     Navigator.push(
          //       context,
          //       SlideRoute(HomePage(), milliseconds: 500),
          //     );
          //   }
          // } else {
            Navigator.push(
              context,
              SlideRoute(HomePage(), milliseconds: 500),
            );
          // }
        },
        minWidth: Dimension.w300,
        height: Dimension.h50,
        shape: RoundedRectangleBorder(
          side: BorderSide(color: Colors.white, width: Dimension.w1),
          borderRadius: BorderRadius.all(Radius.circular(Dimension.r25)),
        ),
        child: Text(
          DemoLocalizations.of(context).login,
          style: TextStyle(fontSize: Dimension.sp24, color: Colors.black),
        ),
      );

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage("assets/images/login_bg.jpg"), fit: BoxFit.cover),
      ),
      child: SingleChildScrollView(
        child: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height - kToolbarHeight,
          child: Center(
            child: Container(
              height: Dimension.h200,
              child: Form(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    _buildAccountTextField(),
                    PSWTextFormField(),
                    _buildSubmitButton(),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

///密码输入框
class PSWTextFormField extends StatefulWidget {
  @override
  _PSWTextFormFieldState createState() => _PSWTextFormFieldState();
}

class _PSWTextFormFieldState extends State<PSWTextFormField> {
  ///是否隐藏密码
  bool obscureText = true;

  ///密码编辑控制器
  TextEditingController _pswController = TextEditingController();

  @override
  void dispose() {
    ///释放密码编辑控制器
    _pswController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: Dimension.h50,
      width: Dimension.w300,
      child: TextFormField(
        textAlignVertical: TextAlignVertical.bottom,
        obscureText: obscureText,
        style: TextStyle(fontSize: Dimension.sp24),
        decoration: InputDecoration(
          prefixIcon: Icon(
            Icons.lock,
            color: Colors.black,
            size: Dimension.sp24,
          ),
          prefixIconConstraints: BoxConstraints(
              minWidth: Dimension.sp48, minHeight: Dimension.sp48),
          suffixIcon: IconButton(
            icon: !obscureText
                ? Icon(
                    Icons.visibility,
                    size: Dimension.sp24,
                  )
                : Icon(
                    Icons.visibility_off,
                    size: Dimension.sp24,
                  ),
            onPressed: () => setState(() => obscureText = !obscureText),
          ),
          suffixIconConstraints: BoxConstraints(
              minWidth: Dimension.sp48, minHeight: Dimension.sp48),
          // prefixText: "密码",
          hintText: DemoLocalizations.of(context).pswHint,
          hintStyle: TextStyle(color: Colors.white, fontSize: Dimension.sp24),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.white, width: Dimension.w1),
            borderRadius: BorderRadius.all(Radius.circular(Dimension.r25)),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.black, width: Dimension.w1),
            borderRadius: BorderRadius.all(Radius.circular(Dimension.r25)),
          ),
        ),
      ),
    );
  }
}
